pipe.ts ➔ exclusion   A
last analyzed

Complexity

Conditions 3

Size

Total Lines 27
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 27
rs 9.352
c 0
b 0
f 0
cc 3
1
import { ClassHash } from "../../src/main.types"
2
3
// Nope
4
// type tExcluder<S extends Record<string, ClassHash>>
5
// = (
6
//   <E extends{[K in keyof S]?: ClassHash}>(exclude: E) => typeof exclude extends Record<infer E, ClassHash>
7
//   ? { [P in Exclude<keyof S, keyof E>]: ClassHash; } & tExcluder<{ [P in Exclude<keyof S, keyof E>]: ClassHash; }>
8
//   : never
9
// );
10
11
12
// type tExcluder<S extends Record<string, ClassHash>>
13
// = (
14
//   <E extends Record<string, ClassHash>>(exclude: E) => { [P in Exclude<keyof S, keyof E>]: ClassHash; }
15
//   & tExcluder<{ [P in Exclude<keyof S, keyof E>]: ClassHash; }>
16
// );
17
18
// type tExcluder<S extends Record<string, ClassHash>>
19
// = (
20
//   <E extends Record<string, ClassHash>>(exclude: E) => keyof E extends keyof S
21
//   ? { [P in Exclude<keyof S, keyof E>]: ClassHash; }
22
//     & tExcluder<{ [P in Exclude<keyof S, keyof E>]: ClassHash; }>
23
//   : {[P in Exclude<keyof E, keyof S>]: never;}
24
// );
25
26
type tExcluder<S extends Record<string, ClassHash>>
27
= (
28
  <E extends {[K in keyof S]?: ClassHash}>(exclude: E) =>
29
  //  keyof E extends keyof S ?
30
   { [K in Exclude<keyof S, keyof E>]: ClassHash; }
31
    & tExcluder<{ [P in Exclude<keyof S, keyof E>]: ClassHash; }>
32
  // : {[P in Exclude<keyof E, keyof S>]: never;}
33
);
34
35
36
function exclusion<
37
  S extends Record<string, ClassHash>,
38
  E extends {[K in keyof S]?: ClassHash}
39
>(
40
  source: S, ex: E
41
) {
42
43
  const filtered = {...source}
44
  for (const k in ex) {
45
    delete filtered[k]
46
  }
47
48
  const host = (
49
    e: { [P in Exclude<keyof S, keyof E>]?: ClassHash; }
50
  ) => exclusion(
51
    filtered as { [P in Exclude<keyof S, keyof E>]: ClassHash; },
52
    e
53
  )
54
55
  for (const key in filtered)
56
    //@ts-expect-error
57
    host[key]
58
    = filtered[key]
59
60
  return host as tExcluder<{ [P in Exclude<keyof S, keyof E>]: ClassHash; }>
61
}
62
63
const source: Record<"a"|"b"|"c"|"d"|"e", ClassHash> = {a: "a", b: undefined, c: "c", d: undefined, e: undefined}
64
65
const step0 = exclusion(
66
  source,
67
  //@ts-expect-error 'z' does not exist in type
68
  {z: undefined}
69
)
70
, answ0: typeof step0 = {
71
  //@ts-expect-error
72
  whatever: true
73
}
74
, step1 = exclusion(source, {a: "a", b: undefined})
75
76
//@ts-expect-error
77
step1({"c": undefined, "z": undefined})
78
()
79
80
const step2 = step1({"c": undefined})
81
//@ts-expect-error
82
, {c} = step2
83
, step3 = {...step2({
84
  //@ts-expect-error
85
  "z": ""
86
})}
87
, result = {...step2}
88
, checks: Record<string, typeof result> = {
89
  "output": {d: "", e: ""},
90
  "unknown": {
91
    //@ts-expect-error
92
    unknown: ""
93
  },
94
  //@ts-expect-error
95
  "lost": {
96
    d: ""
97
  },
98
  "previously ommited": {
99
    d: "", e: "",
100
    //@ts-expect-error
101
    a: ""
102
  }
103
}
104
105
const unknown0 = exclusion({} as Record<string, ClassHash>, {a: "a"})
106
//todo @ts-error
107
, unknown1 = unknown0({a: "a"})
108
109
export {answ0, step3, checks, unknown1}